<?php

    declare(strict_types=1);
    
    namespace app\common\service\crm;
    
    use app\common\traits\ServiceTrait;
    use app\common\dao\crm\SupplierDao;
    
    /**
     * 供应商表 服务业务层(Service)
     * @author 龙
     * @date  2022-07-16
     * @copyright ©2022 泉州市盛筑信息科技有限公司 版权所有
     */
    class SupplierService
    {
      protected $dao = null;
    
      public function __construct()
      {
        $this->dao = new SupplierDao();
      }
      use ServiceTrait;
     /**
   * 重写 输出格式化
   */
  public function _list_format($list)
  {
    foreach ($list as $k => &$v) {
      if (isset($v['create_time'])) {
        $list[$k]['create_time'] = time2str($v['create_time']);
      }
    }
    return $list;
  }
  /**
   * 添加编辑数据方法
   * @return array
   */
  public function saveData($data)
  {
    $result = [
      'status' => false,
      'data'   => '',
      'msg'    => ''
    ];
    //判断是新增还是修改
    if (isset($data['id'])) {
      $carInfo = $this->dao->getInfoById($data['id']);
      if (!$carInfo) {
        return _err('查无记录');
      }
      $data['status'] = checkon('status', $data);
      //更新数据库
    } else {  //添加
      $data['status']  = checkon('status', $data);
      //定义一个空数组 
      $where = [];
      $where['supplier_name'] = $data['supplier_name'];
      $result1 = $this->getInfoByWhereSupplierName($where);
      if (!empty($result1)) {
        return _err('供应商名称已经存在，请重新输入');
      }
      $data['status'] = checkon('status', $data);
    }
    return $this->saveForm($data);
  }
  /**
   * 重写 搜索条件
   * @param $post
   * @return mixed
   */
  public function _list_where($post)
  {
    $where = [];
    $order = ['id' => 'desc'];
    $field = ['*'];
    // if (isset($post['search_date']) && !empty($post['search_date'])) {
    //   $date_string = $post['search_date'];
    //   $date_array = explode('到', urldecode($date_string));
    //   $stime = strtotime($date_array[0] . '00:00:00');
    //   $etime = strtotime($date_array[1] . '23:59:59');
    //   $where[] = ['entry_time', 'between', [$stime, $etime]];
    // }
    if (isset($post['search_code']) && $post['search_code'] != "") {
      $where[] = ['code', '=', $post['search_code']];
    }
    if (isset($post['keywords']) && !empty($post['keywords'])) {
      $where[] = ['supplier_name', 'like', '%' . $post['keywords'] . '%'];
    }
    if (isset($post['search_status']) && !empty($post['search_status'])) {
      $where[] = ['status', '=', $post['search_status']];
    }
    if (isset($post['search_sex']) && $post['search_sex'] != "") {
      $where[] = ['sex', '=', $post['search_sex']];
    }
    if (isset($post['search_on_duty']) && $post['search_on_duty'] != "") {
      $where[] = ['on_duty', '=', $post['search_on_duty']];
    }
    if (isset($post['company_id']) && !empty($post['company_id'])) {
      $where[] = ['company_id', '=', $post['company_id']];
    }
    //排序
    if (isset($post['sortfield']) && $post['sortfield'] != "") {
      if (isset($post['sortord']) && $post['sortord'] != "") {
        $order[$post['sortfield']] = $post['sortord'];
      }
    }
    if (isset($post['automatch']) && $post['automatch'] == 1) {
      // $field = "id,real_name as name,real_name,phone,dept_id,dept_id_name,ranking_id,ranking_id_name,super_id,super_name,company_id,entry_time";
      // $where[] = ['status', '=', 1];
      $field = "id,supplier_name";
    }
    $result['where'] = $where;
    $result['field'] = $field;
    $result['order'] = $order;
    return $result;
  }

   //查重
   public function  getInfoByWhereSupplierName($where)
   {
     return $this->dao->getInfoByWhereSupplierName($where);
   }


   /**
   * 获取供应商列表 
   * @return array
   */
  public  function getSupplierNameList()
  {
    $data = $this->dao->getListAll(['status' => 1]);
    return _layui(count($data), $data);
  }
    }
    